﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace NodeEditer
{
    public class EditorNodeWindowBase:EditorWindow
    {
        [MenuItem("CustomTools/Base/EditorNodeWindow")]
        static void OpenWindow()
        {
            EditorNodeWindowBase window =EditorWindow.GetWindow<EditorNodeWindowBase>();
            window.maximized = true;
        }

        public Dictionary<int, EditorNodeData> NodeId2NodeDataRects = new Dictionary<int, EditorNodeData>();

        public List<int> selectedNode = new List<int>();

        public CommandStack<ICustomCommand> CommandStack = new CommandStack<ICustomCommand>();

        public Rect ToolBarRect = Rect.zero;

        public Rect LeftRect=Rect.zero;

        public Rect RightRect = Rect.zero;

        public Rect DownRect = Rect.zero;

        public Vector2 ContextClickPosOffset = new Vector2(10000000, 10000000);
        
        public Vector2 ContextClickPos;
        
        public int CurSelectedNodeId = -1;

        public bool isDraged = true;

        public bool isFirst = true;


        [SerializeField]
        public Texture2D NewNodeTex;


        private void Awake()
        {
            ContextClickPos = ContextClickPosOffset;
            ResolveLayout();
        }

        private void Update()
        {
            if (isDraged)
            {
                ResolveLayout();
            }
        }

        private void OnGUI()
        {
            if (Event.current != null)
            {
                if (Event.current.type == EventType.MouseDrag && Event.current.mousePosition.y < 0) 
                {
                    isDraged = true;
                }
                else
                {
                    if (isDraged)
                    {
                        isDraged = false;
                        ResolveLayout();
                    }
                }

                if (Event.current.type==EventType.MouseDown)
                {
    
                    //Debug.Log(Event.current.mousePosition);
                }

                if (Event.current.control&&Event.current.keyCode==KeyCode.Z)
                {
                    CommandStack.RedoCommand();
                }

                if (Event.current.control && Event.current.keyCode == KeyCode.Y)
                {
                    CommandStack.DoNextCommand();
                }
            }



            EditorGUILayout.BeginVertical();

            DrawToolBar();
            EditorGUILayout.BeginHorizontal();
            DrawLeft();
            DrawRight();
            EditorGUILayout.EndHorizontal();
            DrawDown();
            EditorGUILayout.EndVertical();
        }

        public virtual void ResolveLayout() { }

        public virtual void DrawToolBar()
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Box("123", EditorStyles.toolbarButton, GUILayout.Width(50), GUILayout.Height(20));

            if (GUILayout.Button("test"))
            {

                Debug.LogError("123");
            }
            EditorGUILayout.LabelField("test");
            EditorGUILayout.LabelField("test");
            EditorGUILayout.LabelField("test");
            EditorGUILayout.EndHorizontal();

        }

        public virtual void DrawLeft()
        {
            EditorGUILayout.LabelField("test1",GUILayout.Width(200));
        }

        public virtual void DrawRight()
        {
            EditorGUILayout.LabelField("test2");

        }

        public virtual void DrawDown()
        {
            EditorGUILayout.LabelField("test3");
        }

        public virtual void DrawNodes()
        {
            BeginWindows();
            //小窗口绘制
            EndWindows();
        }
    }
}
